<?php
/* @var $this ClienteController */
/* @var $model Cliente */
/* @var $form CActiveForm */
?>

<div class="form">

<?php 
	Yii::app()->clientScript->registerScript('Ative', "
		$('#Cliente_tipoPessoa_0').click(function(){
			$('label[for=\"Cliente_nome_fantasia\"]').html('Nome Fantasía<span class=\"required\">*</span>');
			$('#Cliente_nome_fantasia').val('');
			
			$('label[for=\"Cliente_cnpj\"]').html('Cnpj <span class=\"required\">*</span>');
			$('#Cliente_cnpj').val('');
			
			$('label[for=\"Cliente_razaoSocial\"]').show();
			$('#Cliente_razaoSocial').show();
			$('#Cliente_razaoSocial').val('');
			
			$('label[for=\"Cliente_ie\"]').show();
			$('#Cliente_ie').show();
			$('#Cliente_ie').val('');
			
			$('label[for=\"Cliente_im\"]').show();
			$('#Cliente_im').show();
			$('#Cliente_im').val('');
		});
	");
	
	Yii::app()->clientScript->registerScript('Inative', "
		$('#Cliente_tipoPessoa_1').click(function(){
			$('label[for=\"Cliente_nome_fantasia\"]').html('Nome <span class=\"required\">*</span>');
			$('#Cliente_nome_fantasia').val('');
			
			$('label[for=\"Cliente_cnpj\"]').html('CPF <span class=\"required\">*</span>');
			$('#Cliente_cnpj').val('');
			
			$('label[for=\"Cliente_razaoSocial\"]').hide();
			$('#Cliente_razaoSocial').hide();
			$('#Cliente_razaoSocial').val('');
			
			$('label[for=\"Cliente_ie\"]').hide();
			$('#Cliente_ie').hide();
			$('#Cliente_ie').val('');
			
			$('label[for=\"Cliente_im\"]').hide();
			$('#Cliente_im').hide();
			$('#Cliente_im').val('');
		});
	");
	
	if($model->tipoPessoa == 'f')
	{
		Yii::app()->clientScript->registerScript('InativeReload', "
			$('label[for=\"Cliente_nome_fantasia\"]').html('Nome <span class=\"required\">*</span>');
			
			$('label[for=\"Cliente_cnpj\"]').html('CPF <span class=\"required\">*</span>');
			
			$('label[for=\"Cliente_razaoSocial\"]').hide();
			$('#Cliente_razaoSocial').hide();
			
			$('label[for=\"Cliente_ie\"]').hide();
			$('#Cliente_ie').hide();
			
			$('label[for=\"Cliente_im\"]').hide();
			$('#Cliente_im').hide();
		");
	}
	
	$form=$this->beginWidget('CActiveForm', array(
		'id'=>'cliente-form',
		// Please note: When you enable ajax validation, make sure the corresponding
		// controller action is handling ajax validation correctly.
		// There is a call to performAjaxValidation() commented in generated controller code.
		// See class documentation of CActiveForm for details on this.
		'enableAjaxValidation'=>false,
		'htmlOptions' => array('enctype' => 'multipart/form-data'),
	)); 
?>

	<p class="note">Fields with <span class="required">*</span> are required.</p>

	<?php echo $form->errorSummary($model); ?>

	<div class="row">
		<?php echo $form->labelEx($model,'tipoPessoa'); ?>
		<?php 
			echo $form->radioButtonList($model,'tipoPessoa', array('j' => 'Jurídica', 'f' => 'Física'), array(
				'labelOptions' => array('style' => 'display:inline'),
				'separator' => ' '
			));
		?>
		<?php echo $form->error($model,'tipoPessoa'); ?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model,'razaoSocial'); ?>
		<?php echo $form->textField($model,'razaoSocial',array('size'=>60,'maxlength'=>60)); ?>
		<?php echo $form->error($model,'razaoSocial'); ?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model,'nome_fantasia'); ?>
		<?php echo $form->textField($model,'nome_fantasia',array('size'=>60,'maxlength'=>60)); ?>
		<?php echo $form->error($model,'nome_fantasia'); ?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model,'cnpj'); ?>
		<?php echo $form->textField($model,'cnpj',array('size'=>15,'maxlength'=>15)); ?>
		<?php echo $form->error($model,'cnpj'); ?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model,'ie'); ?>
		<?php echo $form->textField($model,'ie',array('size'=>20,'maxlength'=>20)); ?>
		<?php echo $form->error($model,'ie'); ?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model,'im'); ?>
		<?php echo $form->textField($model,'im',array('size'=>20,'maxlength'=>20)); ?>
		<?php echo $form->error($model,'im'); ?>
	</div>
	
	<div class="row">
		<?php echo $form->labelEx($model,'logo'); ?>
		<?php 
			if(!$model->isNewRecord)
			{
				echo CHtml::image(Yii::app()->baseUrl . '/images/logo/' . $model->logo, 'Logo', array('width' => 100));
				echo "<br />";
				echo $form->radioButtonList($model, 'alterarFoto', array(0 => 'Não', 1 => 'Sim'), array(
					'labelOptions' => array('style' => 'display:inline'),
					'separator' => ' '
				));
  				echo "<br />";
			}
			echo $form->fileField($model,'logo',array('size'=>10,'maxlength'=>10)); 
		?>
		<?php echo $form->error($model,'logo'); ?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model,'status'); ?>
		<?php echo $form->dropDownList($model,'status', array(0=> 'Inativo', 1 => 'Ativo')); ?>
		<?php echo $form->error($model,'status'); ?>
	</div>

	<?php
		if(!$model->isNewRecord)
		{
			/*
			$colorBox = $this->widget('application.extensions.colorpowered.JColorBox');
			
			$colorBox
			    ->addInstance('.colorBoxEndereco', array('iframe'=>true, 'width'=>'80%', 'height'=>'80%'));
			*/
			
			echo CHtml::link('Cadastro Endereco', array('endereco/createEndereco', 'id' => base64_encode($model->id), 'return' => base64_encode('usuario/update')), array('class' => 'colorBoxEndereco'));

			$modelEndereco =Endereco::model();
			$this->widget('zii.widgets.grid.CGridView', array(
				'id'=>'endereco-grid',
				'dataProvider'=>$modelEndereco->search(array(
					'cliente_id' => $model->id
				)),
				'filter'=>$modelEndereco,
				'columns'=>array(
					'bairro',
					'numero',
					'cep',
					'complemento',
					/*
					'usuario_id',
					'pais_id',
					'uf_id',
					'cidade_id',
					'excluido',
					'dataCadastro',
					*/
					array(
						'class'=>'CButtonColumn',
						'template'=>'{update} {view} {delete}',
				        'buttons'=>array (
				            'update'=> array(
				                'label'=>'edit',
				                'imageUrl'=>'',
				                'url'=>'Yii::app()->createUrl("endereco/update", array("id"=>$data->id, "uid" => $data->cliente_id))',
				                'options'=>array('class'=>'icon-edit' ),
				            ),
				            'view'=>array(
				                'label'=>'',
				                'imageUrl'=>'',
				                'url'=>'Yii::app()->createUrl("endereco/view", array("id"=>$data->id))',
				                'options'=>array( 'class'=>'icon-search' ),
				            ),
				            'delete'=>array(
				                'label'=>'',
				                'imageUrl'=>'',
				                'url'=>'Yii::app()->createUrl("endereco/delete", array("id"=>$data->id))',
				                'options'=>array( 'class'=>'icon-remove' ),
				            ),
				        ),
					),
				),
			));
			
			echo CHtml::link('Cadastro Telefone', array('telefone/createTelefone', 'id' => base64_encode($model->id), 'return' => base64_encode('usuario/update')), array('class' => 'colorBoxEndereco'));
			
			$telefoneModel = Telefone::model();
			$this->widget('zii.widgets.grid.CGridView', array(
				'id'=>'telefone-grid',
				'dataProvider'=>$telefoneModel->search(),
				'filter'=>$telefoneModel,
				'columns'=>array(
					'ddd',
					'numero',
					array(
						'name' => 'resumo',
						'value' => 'Telefone::getNomeResumo($data->resumo)'
					),//0 = Casa, 1=Comercio, 2=Celular
					array(
						'class'=>'CButtonColumn',
						'template'=>'{update} {view} {delete}',
				        'buttons'=>array (
				            'update'=> array(
				                'label'=>'edit',
				                'imageUrl'=>'',
				                'url'=>'Yii::app()->createUrl("telefone/update", array("id"=>$data->id, "uid" => $data->cliente_id))',
				                'options'=>array('class'=>'icon-edit' ),
				            ),
				            'view'=>array(
				                'label'=>'',
				                'imageUrl'=>'',
				                'url'=>'Yii::app()->createUrl("endereco/view", array("id"=>$data->id))',
				                'options'=>array( 'class'=>'icon-search' ),
				            ),
				            'delete'=>array(
				                'label'=>'',
				                'imageUrl'=>'',
				                'url'=>'Yii::app()->createUrl("endereco/delete", array("id"=>$data->id))',
				                'options'=>array( 'class'=>'icon-remove' ),
				            ),
				        ),
					),
				),
			));
			
			echo CHtml::link('Cadastro Endereco Virtual', array('enderecovirtual/createEnderecoVirtual', 'id' => base64_encode($model->id), 'return' => base64_encode('usuario/update')), array('class' => 'colorBoxEndereco'));
			
			$enderecoVirtualModel = Enderecovirtual::model();
			$this->widget('zii.widgets.grid.CGridView', array(
				'id'=>'enderecovirtual-grid',
				'dataProvider'=>$enderecoVirtualModel->search(),
				'filter'=>$enderecoVirtualModel,
				'columns'=>array(
					'nome',
					array(
						'name' => 'tipoEnderecoVirtual_id',
						'value' => 'Tipoenderecovirtual::getNameTipoEndereco($data->tipoEnderecoVirtual_id)'
					),
					array(
						'name' => 'dataCadastro',
						'value' => 'date("d/m/Y H:i:s", strtotime($data->dataCadastro))'
					),
					array(
						'class'=>'CButtonColumn',
						'template'=>'{update} {view} {delete}',
				        'buttons'=>array (
				            'update'=> array(
				                'label'=>'edit',
				                'imageUrl'=>'',
				                'url'=>'Yii::app()->createUrl("enderecoVirtual/update", array("id"=>$data->id, "uid" => $data->cliente_id))',
				                'options'=>array('class'=>'icon-edit' ),
				            ),
				            'view'=>array(
				                'label'=>'',
				                'imageUrl'=>'',
				                'url'=>'Yii::app()->createUrl("endereco/view", array("id"=>$data->id))',
				                'options'=>array( 'class'=>'icon-search' ),
				            ),
				            'delete'=>array(
				                'label'=>'',
				                'imageUrl'=>'',
				                'url'=>'Yii::app()->createUrl("endereco/delete", array("id"=>$data->id))',
				                'options'=>array( 'class'=>'icon-remove' ),
				            ),
				        ),
					),
				),
			));
		}
	?>

	<div class="row buttons">
		<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
	</div>
	
<?php $this->endWidget(); ?>

</div><!-- form -->